package com.zl.registration.controller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.zl.registration.entity.ActivityInfo;
import com.zl.registration.module.result.Result;
import com.zl.registration.service.ActivityInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动内容接口
 *
 * @author zoulei
 * @date 2021/11/30 16:38
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Resource
    private ActivityInfoService activityInfoService;

    /**
     * 查询活动列表
     *
     * @return 活动列表
     */
    @GetMapping("/list")
    public Result<PageInfo<ActivityInfo>> activityList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                       @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex) {
        PageMethod.startPage(pageIndex, pageSize);
        List<ActivityInfo> list = activityInfoService.list();
        PageInfo<ActivityInfo> activityPageInfo = new PageInfo<>(list);
        return Result.success(activityPageInfo);
    }

    /**
     * TODO 接入Elastic Search 实现高级模糊查询
     * 根据活动名称查找活动
     *
     * @param name 活动名称
     * @return 活动列表
     */
    @GetMapping("/findByName")
    public Result<List<ActivityInfo>> findActivityByName(String name) {
        Map<String, Object> conditions = new HashMap<>(1);
        conditions.put("name", name);
        List<ActivityInfo> activityInfos = activityInfoService.listByMap(conditions);
        return Result.success(activityInfos);
    }
}
